﻿using ARS.Common.Helper;
using ARS.Common.Service;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace ARS.Service
{
    public class Tester
    {
        //public static void TestEncrypt()
        //{
        //    Console.WriteLine("测试加解密!");
        //    var name = "Jeffcky";
        //    Console.WriteLine($"加密字符串为{name}");
        //    var encryptStr = EncryptHelper.DesEncrypt(name, "sblw-3hn8-sqoy19");
        //    Console.WriteLine($"加密后结果为：{encryptStr}");
        //    var decryptStr = EncryptHelper.DesDecrypt(encryptStr, "sblw-3hn8-sqoy19");
        //    Console.WriteLine($"解密后字符串为{decryptStr}");


        //    Console.WriteLine($"加密字符串为{name}");
        //    var AESencryptStr = EncryptHelper.AESEncrypt(name, "qaszxcdqaszxcdee");
        //    Console.WriteLine($"加密后结果为：{AESencryptStr}");
        //    var AESdecryptStr = EncryptHelper.AESDecrypt(encryptStr, "qaszxcdqaszxcdee");
        //    Console.WriteLine($"解密后字符串为{AESdecryptStr}");

        //}

        public static void TestSendEmail()
        {
            Console.WriteLine("测试邮件发送!");
            var config = new SmtpSetting()
            {
                Name = "可得集团-大数据组",
                Password = "wpj19900210",
                User = "wangjunyang@kede.com",
                Server = "smtp.kede.com"
            };
            MailSenderHelper.Init(config);
            MailSenderHelper.Send(new[] { "merlinking@aliyun.com" }, new[] { "593784063@qq.com" }, new[] { "merlinking@dingtalk.com", }, "2018年数据报告", "你好，这是18年的数据 两个邮箱");
            var files = new[] { @"D:\Repository\autoreportservice\ARS\ARS.Service\bin\Debug\netcoreapp3.0\exportExcel\定制+售后6OYSPFZD.xlsx", @"D:\Repository\autoreportservice\ARS\ARS.Service\bin\Debug\netcoreapp3.0\exportExcel\我的测试数据D8DDJGNP.xlsx" };
            MailSenderHelper.Send(null, new[] { "merlinking@aliyun.com" }, new[] { "593784063@qq.com" }, new[] { "merlinking@dingtalk.com", }, "2018年数据报告-带附件", "你好，这是18年的数据 两个邮箱", files);
            config.User = "wangjunyang";
            config.Name = "小蜜蜂";
            //  MailSenderHelper.Init(config);
            //  MailSenderHelper.Send(new[] { "merlinking@aliyun.com" }, "2018年数据报告", "你好，这是18年的数据");
            Console.WriteLine("测试邮件发送 完成!");
        }



        public static void GetDirs()
        {
            Log.WriteLine($"获取当前执行目录", ConsoleColor.White);

            Log.WriteLine($"获取当前执行目录 AppContext.BaseDirectory:{AppContext.BaseDirectory}", ConsoleColor.DarkRed);
            Log.WriteLine($"获取当前执行目录 Environment.CurrentDirectory:{Environment.CurrentDirectory}", ConsoleColor.DarkRed);
            Log.WriteLine($"获取当前执行目录 Directory.GetCurrentDirectory:{Directory.GetCurrentDirectory()}", ConsoleColor.DarkRed);
            Log.WriteLine($"获取当前执行目录 Assembly.GetEntryAssembly().Location:{Assembly.GetEntryAssembly().Location}", ConsoleColor.DarkRed);
            Log.WriteLine($"获取当前执行目录 Assembly.GetExecutingAssembly().Location:{Assembly.GetExecutingAssembly().Location}", ConsoleColor.DarkRed);
            Log.WriteLine($"----------------------------------------", ConsoleColor.White);
        }

        public static void TestData2Execl()
        {
            Log.WriteLine($"测试生成Excel文件", ConsoleColor.White);

            var ds = new DataSet("我的测试数据");
            var dt1 = new DataTable("定制+售后");

            dt1.Columns.Add("Id", typeof(int));
            dt1.Columns.Add("Name", typeof(string));
            dt1.Columns.Add("Gender", typeof(bool));
            dt1.Columns.Add("Age", typeof(int));
            dt1.Columns.Add("BrithDay", typeof(DateTime));
            for (var i = 0; i < 799; i++)
            {
                dt1.Rows.Add(new object[] { i, "定制" + StringHelper.Getcode(5), i % 3 == 1, 18, DateTime.Now.AddSeconds(-i) });
            }

            var dt2 = new DataTable("非定制+售后");
            dt2.Columns.Add("Id", typeof(int));
            dt2.Columns.Add("Name", typeof(string));
            dt2.Columns.Add("Gender", typeof(bool));
            dt2.Columns.Add("Age", typeof(int));
            dt2.Columns.Add("BrithDay", typeof(DateTime));
            for (var i = 0; i < 144; i++)
            {
                dt2.Rows.Add(new object[] { i, "非定制定制" + StringHelper.Getcode(5), i % 3 == 1, 18, DateTime.Now.AddSeconds(-i) });
            }
            ds.Tables.Add(dt1);
            ds.Tables.Add(dt2);
            var filename = Data2ExeclHelper.DataSet2Execl(ds);

            var filename2 = Data2ExeclHelper.DataTable2Execl(dt1);
            Log.WriteLine($"测试生成Excel文件完成  文件地址：{filename}", ConsoleColor.Red);
            Log.WriteLine($"测试生成Excel文件完成  文件地址：{filename2}", ConsoleColor.Red);
        }



        private static string pg_sql = "Server=192.168.100.80;Port=5432;User Id=gpadmin;Password=pga*%2134Kmjg531#db;Database=kede_fv;CommandTimeout=0";
        private static string sql_sql = "data source=192.168.100.153,1433;initial catalog=TiDB;persist security info=True;user id=wangjunyang;password=kdcfldsgfabci_.SWER233;MultipleActiveResultSets=True;";

        public static void TestGetDataSet()
        {

            Log.WriteLine($"获取多表数据测试", ConsoleColor.White);
            IExecScript execScript = new Common.Service.ExecScripts.PgExecScript();

            var ds1 = execScript.ExecSql(pg_sql, @"SELECT  * FROM ori_SalePlatform LIMIT 10;
SELECT  * FROM ori_Goods  LIMIT 10;");




            IExecScript execScript2 = new Common.Service.ExecScripts.SqlExecScript();

            var ds2 = execScript2.ExecSql(sql_sql, @"SELECT TOP 10 * FROM dbo.ori_SalePlatform;SELECT TOP 10 * FROM dbo.ori_Goods;");

            Log.WriteLine($"获取多表数据测试 end", ConsoleColor.White);



        }


        public static void TestSendServer()
        {
            string pg_sql = "Server=192.168.117.201;Port=5432;User Id=gpadmin;Password=gpadmin;Database=AutoReport;CommandTimeout=0";
            BuilderServices.Init(pg_sql);
            var _params = new List<KeyValuePair<string, string>>();
            _params.Add(new KeyValuePair<string, string>("p_startTime", "2019-01-01"));
            _params.Add(new KeyValuePair<string, string>("p_endTime", "2019-10-01"));
            //{p_startTime,p_endTime}

            BuilderServices.Run(1, _params);
            // BuilderServices.Run(2,);



        }
        public static void TestOutputText()
        {
            Console.WriteLine("日志输出");
            for (var i = 0; i < 23; i++)
            {
                Console.WriteLine($"执行 {StringHelper.Getcode(8)} {DateTime.Now}");
                Thread.Sleep(500);
            }
            Console.WriteLine("日志输出完成");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="commond"></param>
        /// <param name="_param"></param>
        /// <param name="useShellExecute">区别，不能进行数据输出</param>
        /// <param name="outputConsonle">是否输出到控制台</param>
        public static void TestCommand(string commond = "dotnet", string _param = "--info")
        {
            try
            {
                Log.WriteLine($"------执行命令-----------\r\n {commond} {_param}\r\n", ConsoleColor.Yellow);
                ProcessStartInfo psi = null;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    psi = new ProcessStartInfo(commond, _param) { RedirectStandardOutput = true };

                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    psi = new ProcessStartInfo
                    {
                        FileName = "/bin/bash",
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        Arguments = string.Format($"-c \"{commond} {_param}")
                    };
                }
                //启动
                if (psi == null)
                    return;
                var proc = Process.Start(psi);
                if (proc == null)
                {
                    Console.WriteLine("Can not exec.");
                }
                else
                {
                    
                    using (var sr = proc.StandardOutput)
                    {
                        while (!sr.EndOfStream)
                        {
                            Console.WriteLine(sr.ReadLine());
                        }
                    }
                    if (!proc.HasExited)
                    {
                        proc.Kill();
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine($"Message:{ex.Message}\r\nStackTrace: {ex.StackTrace}");
            }
        }

       
    }
}
